.text
.section	.rodata
	.align	3
.LC2:
	.string	"find the index is %d\n"
	.align	3
.LC3:
	.string	"not found"
	.align	3
.LC0:
	.string	"This is Bo a sample text for Boyer-Moore algorithm."
	.align	3
.LC1:
	.string	"Boyer-Moore"


.text
.globl	badcharTable
.type	badcharTable, @function
badcharTable:
	addi	sp,sp,-64
	sd	ra,56(sp)
	sd	s0,48(sp)
	addi	s0,sp,64
	sd	a0,-56(s0)
	sd	a1,-64(s0)
	ld	a0,-56(s0)
	call	strlen
	mv	a5,a0
	sw	a5,-36(s0)
	sd	zero,-24(s0)
	j	.L2
.L3:
	ld	a5,-24(s0)
	slli	a5,a5,2
	ld	a4,-64(s0)
	add	a5,a4,a5
	lw	a4,-36(s0)
	sw	a4,0(a5)
	ld	a5,-24(s0)
	addi	a5,a5,1
	sd	a5,-24(s0)
.L2:
	ld	a4,-24(s0)
	li	a5,255
	bleu	a4,a5,.L3
	sd	zero,-32(s0)
	j	.L4
.L5:
	ld	a5,-32(s0)
	sext.w	a5,a5
	lw	a4,-36(s0)
	subw	a5,a4,a5
	sext.w	a5,a5
	addiw	a5,a5,-1
	sext.w	a3,a5
	ld	a4,-56(s0)
	ld	a5,-32(s0)
	add	a5,a4,a5
	lbu	a5,0(a5)
	slli	a5,a5,2
	ld	a4,-64(s0)
	add	a5,a4,a5
	sext.w	a4,a3
	sw	a4,0(a5)
	ld	a5,-32(s0)
	addi	a5,a5,1
	sd	a5,-32(s0)
.L4:
	lw	a5,-36(s0)
	addiw	a5,a5,-1
	sext.w	a5,a5
	slli	a5,a5,32
	srli	a5,a5,32
	ld	a4,-32(s0)
	bltu	a4,a5,.L5
	nop
	nop
	ld	ra,56(sp)
	ld	s0,48(sp)
	addi	sp,sp,64
	jr	ra
	
.globl	boyerMoore
.type	boyerMoore, @function
boyerMoore:
	addi	sp,sp,-1072
	sd	ra,1064(sp)
	sd	s0,1056(sp)
	addi	s0,sp,1072
	sd	a0,-1064(s0)
	sd	a1,-1072(s0)
	ld	a0,-1064(s0)
	call	strlen
	mv	a5,a0
	sw	a5,-28(s0)
	ld	a0,-1072(s0)
	call	strlen
	mv	a5,a0
	sw	a5,-32(s0)
	addi	a5,s0,-1056
	mv	a1,a5
	ld	a0,-1072(s0)
	call	badcharTable
	sw	zero,-20(s0)
	j	.L7
.L14:
	lw	a5,-32(s0)
	addiw	a5,a5,-1
	sw	a5,-24(s0)
	j	.L8
.L10:
	lw	a5,-24(s0)
	addiw	a5,a5,-1
	sw	a5,-24(s0)
.L8:
	lw	a5,-24(s0)
	sext.w	a5,a5
	blt	a5,zero,.L9
	lw	a5,-24(s0)
	ld	a4,-1072(s0)
	add	a5,a4,a5
	lbu	a3,0(a5)
	lw	a4,-20(s0)
	lw	a5,-24(s0)
	addw	a5,a4,a5
	sext.w	a5,a5
	mv	a4,a5
	ld	a5,-1064(s0)
	add	a5,a5,a4
	lbu	a5,0(a5)
	mv	a4,a3
	beq	a4,a5,.L10
.L9:
	lw	a5,-24(s0)
	sext.w	a5,a5
	bge	a5,zero,.L11
	lw	a5,-20(s0)
	j	.L15
.L11:
	lw	a4,-20(s0)
	lw	a5,-24(s0)
	addw	a5,a4,a5
	sext.w	a5,a5
	mv	a4,a5
	ld	a5,-1064(s0)
	add	a5,a5,a4
	lbu	a5,0(a5)
	sext.w	a5,a5
	slli	a5,a5,2
	addi	a4,s0,-16
	add	a5,a4,a5
	lw	a5,-1040(a5)
	lw	a4,-24(s0)
	subw	a5,a4,a5
	sext.w	a5,a5
	sext.w	a4,a5
	bgt	a4,zero,.L13
	li	a5,1
.L13:
	sext.w	a5,a5
	lw	a4,-20(s0)
	addw	a5,a4,a5
	sw	a5,-20(s0)
.L7:
	lw	a4,-28(s0)
	lw	a5,-32(s0)
	subw	a5,a4,a5
	sext.w	a4,a5
	lw	a5,-20(s0)
	sext.w	a5,a5
	ble	a5,a4,.L14
	li	a5,-1
.L15:
	mv	a0,a5
	ld	ra,1064(sp)
	ld	s0,1056(sp)
	addi	sp,sp,1072
	jr	ra
	
	.globl	main
	.type	main, @function
main:
	addi	sp,sp,-96
	sd	ra,88(sp)
	sd	s0,80(sp)
	addi	s0,sp,96
	lui	a5,%hi(.LC0)
	addi	a5,a5,%lo(.LC0)
	ld	a6,0(a5)
	ld	a0,8(a5)
	ld	a1,16(a5)
	ld	a2,24(a5)
	ld	a3,32(a5)
	ld	a4,40(a5)
	sd	a6,-72(s0)
	sd	a0,-64(s0)
	sd	a1,-56(s0)
	sd	a2,-48(s0)
	sd	a3,-40(s0)
	sd	a4,-32(s0)
	lw	a5,48(a5)
	sw	a5,-24(s0)
	lui	a5,%hi(.LC1)
	addi	a5,a5,%lo(.LC1)
	ld	a4,0(a5)
	sd	a4,-88(s0)
	lw	a5,8(a5)
	sw	a5,-80(s0)
	addi	a4,s0,-88
	addi	a5,s0,-72
	mv	a1,a4
	mv	a0,a5
	call	boyerMoore
	mv	a5,a0
	sw	a5,-20(s0)
	lw	a5,-20(s0)
	sext.w	a5,a5
	blt	a5,zero,.L17
	lw	a5,-20(s0)
	mv	a1,a5
	lui	a5,%hi(.LC2)
	addi	a0,a5,%lo(.LC2)
	call	printf
	j	.L18
.L17:
	lui	a5,%hi(.LC3)
	addi	a0,a5,%lo(.LC3)
	call	puts
.L18:
	li	a5,0
	mv	a0,a5
	ld	ra,88(sp)
	ld	s0,80(sp)
	addi	sp,sp,96
	jr	ra
